package com.lt.setimage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.PowerManager;
import android.provider.MediaStore;
import android.support.annotation.IdRes;
import android.support.v7.app.AppCompatActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, RadioGroup.OnCheckedChangeListener {
    ImageView iv_show;
    TextView tv_right;
    RadioGroup rg_tab;
    DisplayMetrics metrics;
    private PowerManager.WakeLock wakeLock = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        iv_show = (ImageView) findViewById(R.id.iv_show);
        tv_right = (TextView) findViewById(R.id.tv_right);
        rg_tab = (RadioGroup) findViewById(R.id.rg_tab);
       //tv_right.setOnClickListener(this);
        rg_tab.setOnCheckedChangeListener(this);
         metrics= new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        initView(25);

        setLight(this,225);
    }
private void  initView(float k){

    float f=  Utils.applyDimension(Utils.COMPLEX_UNIT_MM,k,metrics);
    Log.i("imageview", "width: " + f);
    int i= (int) f;
    RelativeLayout.LayoutParams layoutParams=new RelativeLayout.LayoutParams(i,i);
    layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);//addRule参数对应RelativeLayout XML布局的属性

    iv_show.setLayoutParams(layoutParams);
}
    @Override
    public void onClick(View v) {
       if( MySystemPermissions.setStoragePermissions(MainActivity.this,"权限申请","请允许访问您设备上的照片")){
            Intent intent1 = new Intent(Intent.ACTION_PICK, null);
            intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
            startActivityForResult(intent1, 1);
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 1:

                if (data != null) {
                    try {
                        Uri selectedImage = data.getData();
                        String[] filePathColumns = {MediaStore.Images.Media.DATA};
                        Cursor c = MainActivity.this.getContentResolver().query(
                                selectedImage, filePathColumns, null, null, null);
                        c.moveToFirst();
                        int columnIndex = c.getColumnIndex(filePathColumns[0]);
                        String picturePath = c.getString(columnIndex);
                        c.close();
                        // 获取图片并显示
                        Bitmap bm = Bimp.revitionImageSize(picturePath);
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                        iv_show.setImageBitmap(bm);

                    } catch (IOException e) {
                        e.printStackTrace();
                        Log.d("setimage",e.getMessage());
                    }
                  //  java.io.FileNotFoundException: /storage/emulated/0/360Download/24aba879aae9d9d9d73aa0a9e80c03b5.png: open failed: EACCES (Permission denied)
                }


        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
        switch (checkedId) {
            case R.id.tab1:
                initView(25);
                break;
            case R.id.tab2:
                initView(30);
                break;
            case R.id.tab3:
                initView(32);
                break;
            case R.id.tab4:
                initView(35);
                break;
            case R.id.tab5:
                initView(40);
                break;
        }
    }
    // 处理权限申请的回调
    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case MySystemPermissions.STORAGES_REQUEST: {
                if (grantResults.length > 0
                        && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // 授权成功
                    Intent intent1 = new Intent(Intent.ACTION_PICK, null);
                    intent1.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
                    startActivityForResult(intent1, 1);
                    // ToastUtil.showShort("授权成功");
                } else {
                    // 授权失败！

                }
                break;
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        acquireWakeLock(this);

    }

    @Override
    public void onPause() {
        super.onPause();
        releaseWakeLock();

    }


    public void acquireWakeLock(Context context) {
        if (wakeLock == null) {
            PowerManager powerManager = (PowerManager) (context
                    .getSystemService(Context.POWER_SERVICE));
            wakeLock = powerManager.newWakeLock(
                    PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
            wakeLock.acquire();
        }
    }


    public void releaseWakeLock() {
        if (wakeLock != null && wakeLock.isHeld()) {
            wakeLock.release();
            wakeLock = null;
        }
    }


    private void setLight(Activity context, int brightness) {
        WindowManager.LayoutParams lp = context.getWindow().getAttributes();
        lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);
        context.getWindow().setAttributes(lp);
    }
}
